#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-15 15:43:44
# @Description: Pandas 可以很方便的处理 JSON 数据
##############################################
import json

import pandas as pd
from glom import glom


def run():
    data =[
        {
        "id": "A001",
        "name": "菜鸟教程",
        "url": "www.runoob.com",
        "likes": 61
        },
        {
        "id": "A002",
        "name": "Google",
        "url": "www.google.com",
        "likes": 124
        },
        {
        "id": "A003",
        "name": "淘宝",
        "url": "www.taobao.com",
        "likes": 45
        }
    ]
    df = pd.DataFrame(data)
    print(df)
    print('-------------------------------------------------------------------------------------------------')
    # 字典格式的 JSON                                                                                              
    s = {
        "col1":{"row1":1,"row2":2,"row3":3},
        "col2":{"row1":"x","row2":"y","row3":"z"}
    }

    # 读取 JSON 转为 DataFrame                                                                                          
    df = pd.DataFrame(s)
    print(df)
    
    # 从 URL 中读取 JSON 数据：
    print('-------------------------------------------------------------------------------------------------')
    URL = 'https://static.runoob.com/download/sites.json'
    df = pd.read_json(URL)
    print(df)
    
    print('-------------------------------------------------------------------------------------------------')
    # 读取文件
    df = pd.read_json('sites.json')
    print(df)
    
    print('-------------------------------------------------------------------------------------------------')
    with open('netsted_list.json', 'r') as f:
        data = json.loads(f.read())
    
    # # 展平数据
    # json_normalize() 使用了参数 record_path 并设置为 ['students'] 用于展开内嵌的 JSON 数据 students。
    df_nested_list = pd.json_normalize(data, record_path=['students'], meta=['school_name', 'class'])
    print(df_nested_list)
    
    
    print('-------------------------------------------------------------------------------------------------')
    # 复杂json
    # 使用 Python JSON 模块载入数据
    with open('nested_mix.json','r') as f:
        data = json.loads(f.read())
   
    df = pd.json_normalize(
        data,
        record_path =['students'],
        meta=[
            'class',
            ['info', 'president'],
            ['info', 'contacts', 'tel']
        ]
    )
    print(df)
    print('-------------------------------------------------------------------------------------------------')
    # glom 模块允许我们使用 . 来访问内嵌对象的属性。
    df = pd.read_json('nested_deep.json')
    data = df['students'].apply(lambda row: glom(row, 'grade.math'))
    print(data)

if __name__ == '__main__':
   run()
